
const map = new WeakMap();

let ob: ResizeObserver = null

export default {
    mounted(el: any, binding: any) {
        console.log("初始化")
        if (!ob) {
            ob = new ResizeObserver((entries) => {
                for (const entry of entries) {
                    const handler = map.get(entry.target);
                    if (handler) {
                        const box = entry.borderBoxSize[0]
                        handler({
                            width: box.inlineSize,
                            height: box.blockSize
                        })
                    }
                }
            });
        }
        // 监控尺寸变化
        ob.observe(el);
        map.set(el, binding.value)
    },
    unmounted(el: any) {
        // 移除监听
        ob.unobserve(el);
    }
}
